/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree.cellRenderer;

import java.awt.Component;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.jxSwing.component.tree.node.IconNode;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree.cellRenderer
 * @date 13/02/2014 - 20:53:46
 */
public class IconNodeRenderer extends DefaultTreeCellRenderer {
	
	private static final long serialVersionUID = 3161861209103432588L;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
	 */
	@Override
	public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
	
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
		
		Icon icon = ((IconNode)value).getIcon();
		
		//
		if(icon == null) {
			
			final Hashtable<?, ?> icons = (Hashtable<?, ?>)tree.getClientProperty("JTree.icons");
			
			final String iconName = ((IconNode)value).getIconName();
			
			//
			if(icons != null && iconName != null) {
				
				icon = (Icon)icons.get(iconName);
				
				//
				if(icon != null) {
					
					setIcon(icon);
				}
			}
			
		}else {
			
			setIcon(icon);
		}
		
		return this;
	}
}
